//
//  GroupTableViewCell.m
//  etionUI
//
//  Created by wangjian on 8/9/13.
//
//

#import "GroupTableViewCell.h"

#define MARGIN 15
#define DEF_IMAGE_SIZE 30

@interface CGroupTableViewCell()

@property (nonatomic, retain) UIImageView *accessoryView;

@end

@implementation CGroupTableViewCell

@synthesize imageView = _imageView;
@synthesize titleLable = _titleLable;

@synthesize backgroundView = _backgroundView;
@synthesize selectedBackgroundView = _selectedBackgroundView;

@synthesize selectable = _selectable;
@synthesize accessoryType = _accessoryType;

@synthesize accessoryView = _accessoryView;

@synthesize separatorInset = _separatorInset;

@synthesize szTag;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.selectable = YES;
        self.layer.masksToBounds = YES;
        
        self.separatorInset = UIEdgeInsetsZero;
    }
    return self;
}

- (UIImageView *)imageView
{
    if (_imageView == nil)
    {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self addSubview:_imageView];
    }
    return _imageView;
}

- (UILabel *)titleLable
{
    if (_titleLable == nil)
    {
        _titleLable = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLable.backgroundColor = [UIColor clearColor];
        [self addSubview:_titleLable];
    }
    return _titleLable;
}

- (void)setBackgroundView:(UIView *)backgroundView
{
    if (_backgroundView != nil)
    {
        [_backgroundView removeFromSuperview];
    }
    
    UIView *temp = [backgroundView retain_mm];
    [_backgroundView release_mm];
    _backgroundView = temp;
    if (_backgroundView != nil)
    {
        _backgroundView.frame = self.bounds;
        _backgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        [self insertSubview:_backgroundView atIndex:0];
    }
}

- (UIView *)selectedBackgroundView
{
    if (_selectedBackgroundView == nil)
    {
        _selectedBackgroundView = [[UIImageView alloc] initWithFrame:self.bounds];
        ((UIImageView *)_selectedBackgroundView).image = [[UIImage imageNamed:@"gtv_cell_bg_selected"] resizeableCenterImage];
        
        NSUInteger selBgViewIndex = 0;
        if (_backgroundView != nil)
        {
            selBgViewIndex = 1;
        }
        [self insertSubview:_selectedBackgroundView atIndex:selBgViewIndex];
    }
    return _selectedBackgroundView;
}

- (void)setSelectedBackgroundView:(UIView *)selectedBackgroundView
{
    if (_selectedBackgroundView != nil)
    {
        [_selectedBackgroundView removeFromSuperview];
    }
    
    UIView *temp = [selectedBackgroundView retain];
    [_selectedBackgroundView release];
    _selectedBackgroundView = temp;
    if (_selectedBackgroundView != nil)
    {
        _selectedBackgroundView.alpha = 0.0;
        _selectedBackgroundView.frame = self.bounds;
        _selectedBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        NSUInteger selBgViewIndex = 0;
        if (_backgroundView != nil)
        {
            selBgViewIndex = 1;
        }
        [self insertSubview:_selectedBackgroundView atIndex:selBgViewIndex];
    }
}

- (void)setAccessoryType:(UITableViewCellAccessoryType)accessoryType
{
    if (accessoryType == _accessoryType)
    {
        return;
    }
    if (_accessoryView != nil)
    {
        [_accessoryView removeFromSuperview];
        [_accessoryView release];
        _accessoryView = nil;
    }
    _accessoryType = accessoryType;
    if (accessoryType == UITableViewCellAccessoryDisclosureIndicator)
    {
        _accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gtv_cell_disclosure"]];
    }
    [self addSubview:_accessoryView];
}

- (void)dealloc
{
    [_imageView release];
    [_titleLable release];
    
    [_backgroundView release];
    [_selectedBackgroundView release];
    
    [_accessoryView release];
    
    self.szTag=nil;
    
    [super dealloc];
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    CGFloat startX = MARGIN;
    if (_imageView != nil)
    {
        _imageView.frame = CGRectMake(startX, (self.height-_imageView.image.size.height)/2, _imageView.image.size.width, _imageView.image.size.height);
        startX += _imageView.width + MARGIN;
    }
    if (_titleLable != nil)
    {
        CGSize size = [_titleLable.text stringSizeWithFont:_titleLable.font];
        if (_accessoryView == nil)
        {
            _titleLable.frame = CGRectMake(startX, (self.height-size.height)/2, self.width-startX-MARGIN, size.height);
        }
        else
        {
            _titleLable.frame = CGRectMake(startX, (self.height-size.height)/2, self.width-startX-_accessoryView.width-2*MARGIN, size.height);
        }
    }
    if (_accessoryView != nil)
    {
        _accessoryView.frame = CGRectMake(self.width-MARGIN-_accessoryView.width, (self.height-_accessoryView.height)/2, _accessoryView.width, _accessoryView.height);
    }
}

- (void)setSelectedState:(BOOL)bSelected animated:(BOOL)animated
{
    if (self.selectable == NO)
    {
        return;
    }
    if (bSelected == YES)
    {
        if (animated == NO)
        {
            self.selectedBackgroundView.alpha = 1.0;
        }
        else
        {
            self.selectedBackgroundView.alpha = 0.0;
            [UIView animateWithDuration:.3 animations:^{
                self.selectedBackgroundView.alpha = 1.0;
            } completion:^(BOOL finished) {
                
            }];
        }
    }
    else
    {
        if (animated == NO)
        {
            self.selectedBackgroundView.alpha = 0.0;
        }
        else
        {
            [UIView animateWithDuration:.3 animations:^{
                self.selectedBackgroundView.alpha = 0.0;
            } completion:^(BOOL finished) {

            }];
        }
    }
}

- (void)setBackgroundViewWithInversionableImage:(UIImage *)image
{
    UIImageView *backgroundView = [[UIImageView alloc] initWithImage:image];
    self.backgroundView = backgroundView;
    [backgroundView release];
    
    UIImageView *selectedBackgroundView = [[UIImageView alloc] initWithImage:image];
    selectedBackgroundView.transform = CGAffineTransformMakeRotation(M_PI);
    self.selectedBackgroundView = selectedBackgroundView;
    [selectedBackgroundView release];
}

@end
